package wireless.asserrec.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import cm.util.hibernate.HibernateSessionFactory;

public class AssErrRecChart {
	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
	public Session getSession() {
		return session;
	}


	public void setSession(Session session) {
		this.session = session;
	}


	public Transaction getTrans() {
		return trans;
	}


	public void setTrans(Transaction trans) {
		this.trans = trans;
	}


	public String getHql() {
		return hql;
	}


	public void setHql(String hql) {
		this.hql = hql;
	}


	public Query getQuery() {
		return query;
	}


	public void setQuery(Query query) {
		this.query = query;
	}


	public List getL() {
		return l;
	}


	public void setL(List l) {
		this.l = l;
	}
	/*
	 * 产生dateset数据
	 */
	public DefaultCategoryDataset MakeDateset(int i,DefaultCategoryDataset dataset){
		session = HibernateSessionFactory.getSession();
		trans = session.beginTransaction();
		hql = "select frame,percent from AErrecog as sr where sr.aid=:t order by frame";
		query = session.createQuery(hql);
		query.setInteger("t", i);
		l = query.list();
		 for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
	     {
			 Object[] objects=(Object[])sit.next();
			 int frame = (Integer)objects[0];
			 float percent = (Float)objects[1];
			 System.out.print("frame:"+frame);
			 System.out.print("percent:"+percent);
			 dataset.addValue(percent, "第"+i+"个关联误识别率", String.valueOf(frame));
	     }
		return dataset;
	}
	/*
	 * 计算id数量
	 */
	public int CountIds(){
		int number = 0;
		session = HibernateSessionFactory.getSession();
		trans = session.beginTransaction();
		hql="select count(*) from AErrecog as sr group by sr.aid";
		query = session.createQuery(hql);
		l = query.list();
		 for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
	     {
			 number++;
			 System.out.print(number);
	     }
		
		return number;
	}
	
	@SuppressWarnings("deprecation")
	public String execute() throws Exception
	{
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
	  for(int i = 0;i<20;i++)
	  {
		dataset = MakeDateset(i,dataset);
	  }
////////////////////////////				  
				  JFreeChart chart = ChartFactory.createLineChart("关联误识别率", // chart title
						    "次数", // domain axis label
						    "识别率", // range axis label
						    dataset, // data
						    PlotOrientation.VERTICAL, // orientation
						    true, // include legend
						    true, // tooltips
						    false // urls
						    );
				  chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
					//   NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...   
					    CategoryPlot plot = chart.getCategoryPlot();   
					    Font labelFont = new Font("宋体", Font.BOLD, 12);
					    plot.getDomainAxis().setLabelFont(labelFont); //x轴标题字体  
					    plot.getDomainAxis().setTickLabelFont(labelFont); //x轴刻度字体  
					    plot.getRangeAxis().setLabelFont(labelFont);  //y轴外围字体  
					    plot.getRangeAxis().setTickLabelFont(labelFont); //y轴标题字体  
					//   customise the range axis...   
					    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();   
					    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   
					    rangeAxis.setAutoRangeIncludesZero(true); 
					    rangeAxis.setLabelFont(labelFont);
					    rangeAxis.setTickLabelFont(labelFont);
					    rangeAxis.setUpperMargin(0.20);   
					    rangeAxis.setLabelAngle(Math.PI / 2.0);   
					  
					    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();   
					//  renderer.setBaseShape(new Rectangle2D.Double(-1.5, -1.5, 3, 3), true);  //设置点   
					    renderer.setShape(new Rectangle2D.Double(-1.5, -1.5, 3, 3));   
					    renderer.setShapesVisible(true);   
					    renderer.setSeriesPaint(3,Color.red);   
					    renderer.setSeriesLinesVisible(3,false);   
					    renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示折点数据   	
					    chart.getLegend().setItemFont(labelFont);//设置最底下方框内的字体（图示）  
					    HttpServletResponse response = ServletActionContext.getResponse();
						try {
							ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
									450, 400);
							
						} catch (IOException e) {
							e.printStackTrace();
						}
						 System.out.println("成功");
						return null;
	}
}
